﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using MathematicallySafe.HeadCounter.Data.Common;
using MathematicallySafe.HeadCounter.Data.Interfaces;
using MathematicallySafe.HeadCounter.Data;
using StructureMap;

namespace MathematicallySafe.HeadCounter.Services.Exporters
{
    class LuaExport
    {
        private static readonly IUserOptions UserOptions = ObjectFactory.GetInstance<IUserOptions>();
        private static readonly IGuild Guild = ObjectFactory.GetInstance<IGuild>();

        public void Export()
        {
            var hcPath = UserOptions.HeadCounterPath;

            if (!Directory.Exists(hcPath))
                throw new FileNotFoundException("Cannot find HeadCounter Add On path at " + hcPath);

            var raiders = from r in Guild.ActiveRaidersByMain
                          select r;

            var writer = new StreamWriter(hcPath + "\\hcrData.lua");
            writer.WriteLine("hcr = {");

            raiders.ForEach(r => writer.WriteLine("    [\"" + r.MainName + "\"] = { Class=\"" + r.Class +
                                                  "\", Count=\"" + r.ShortPeriodLootCount +
                                                  "\", Rate=\"" + r.ShortPeriodLootRate.ToString("F2") + 
                                                  "\", Priority=\"" + ((double)r.ShortPeriodRelativeLootPriorty).ToString("F2") + 
                                                  "\", Attendance=\"" + (r.ShortPeriodAttendance * 100.0).ToString("F2") + 
                                                  "\", AllAttendance=\"" + (r.ShortPeriodAllAttendance * 100.0).ToString("F2") + 
                                                  "\" },"));

            writer.WriteLine("};");
            writer.Close();
        }
    }
}
